package com.zpxt.zpxt.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zpxt.zpxt.entity.Company;
import com.zpxt.zpxt.entity.Position;
import com.zpxt.zpxt.serve.CompanyServe;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Author:hjx
 * @Description:
 * @Date: Created in 19:43 2018/4/13
 * @Modified By:
 */
@CrossOrigin
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Resource(name="companyServe")
    CompanyServe companyServe;
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
//  增职位
    @RequestMapping("/addPosition")
    @ResponseBody
    public String addPosition(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String info = request.getParameter("data");
        Position position=gson.fromJson(info, Position.class);

        //添加职位信息
        companyServe.addPosition(position);

        //返回结果
        return gson.toJson("OK");
    }
//  改职位
    @RequestMapping("/editPosition")
    @ResponseBody
    public String editPosition(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String info = request.getParameter("data");
        Position position=gson.fromJson(info, Position.class);
        Long companyId = Long.parseLong(request.getParameter("companyId"));

        //查询职位信息
        companyServe.editPosition(position,companyId);

        //返回结果
        return gson.toJson("OK");
    }
//  删除职位
    @RequestMapping("/delPosition")
    @ResponseBody
    public String delPosition(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));
        Long companyId = Long.parseLong(request.getParameter("companyId"));

        //删除职位信息
        companyServe.delPosition(id,companyId);

        //返回结果
        return gson.toJson("OK");
    }
//  查询职位列表
    @RequestMapping("/positionList")
    @ResponseBody
    public List<Position> findPositionList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long companyId = Long.parseLong(request.getParameter("companyId"));

        //查询职位信息
        List<Position> positions = companyServe.findPositionList(companyId);

        //返回结果
        return positions;
    }
//  查询职位
    @RequestMapping("/findPositionOne")
    @ResponseBody
    public Position findPositionOne(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));

        //查询职位信息
        Position position = companyServe.findPositionOne(id);

        //返回结果
        return position;
    }
//    分页查询
    @RequestMapping("/findAll")
    @ResponseBody
    public Page<Position> findAll(
            @PageableDefault(value = 7, sort = { "releaseTime" }, direction = Sort.Direction.DESC) Pageable pageable
    ) throws Exception {
        Page<Position> page = companyServe.findAll(pageable);
        //返回结果
        return page;
    }
    //    分页查询
    @RequestMapping("/findAllLike")
    @ResponseBody
    public Page<Position> findAllLike(
            @PageableDefault(value = 7, sort = { "release_time" }, direction = Sort.Direction.DESC) Pageable pageable,String name
    ) throws Exception {
        Page<Position> page = companyServe.findAllLike(name,pageable);
        //返回结果
        return page;
    }
    // 申请列表
    @RequestMapping("/appList")
    @ResponseBody
    public List<Map> appList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long companyId = Long.parseLong(request.getParameter("companyId"));

        List<Map> list = companyServe.appList(companyId);

        return list;
    }
//    申请处理
    @RequestMapping("/appProcess")
    @ResponseBody
    public String appProcess(HttpServletRequest request, HttpServletResponse response){
        Long appId = Long.parseLong(request.getParameter("appId"));
        String process =request.getParameter("process");
        companyServe.appProcess(appId,process);
        return gson.toJson("OK");
    }
    //   查询公司信息
    @RequestMapping("/findCompanyInfo")
    @ResponseBody
    public Company findCompanyInfo(HttpServletRequest request, HttpServletResponse response){
        Long companyId = Long.parseLong(request.getParameter("companyId"));
        Company company = companyServe.findCompanyInfo(companyId);
        return company;
    }
}
